package com.qwz.inner;

/**
 * @author Qiuwuzhi
 * @version V1.0
 * @Description: 保险合同的对象 - 构建复杂对象
 * @Package com.qwz.inner.InsuranceContract
 * @date 2019/12/16 18:10
 */
public class InsuranceContract {

    private String contractId;
    private String personName;
    private String companyName;
    private long beginDate;
    private long endDate;
    private String otherData;

    private InsuranceContract(ConcreteBuilder builder) {
        this.contractId = builder.contractId;
        this.personName = builder.personName;
        this.companyName = builder.companyName;
        this.beginDate = builder.beginDate;
        this.endDate = builder.endDate;
        this.otherData = builder.otherData;
    }

    /**
     * 构造保险合同对象的构造器，作为保险合同的类级内部类
     */
    public static class ConcreteBuilder {

        private String contractId;
        private String personName;
        private String companyName;
        private long beginDate;
        private long endDate;
        private String otherData;

        public ConcreteBuilder(String contractId, long beginDate, long endDate) {
            this.contractId = contractId;
            this.beginDate = beginDate;
            this.endDate = endDate;
        }

        public ConcreteBuilder setPersonName(String personName) {
            this.personName = personName;
            return this;
        }

        public ConcreteBuilder setCompanyName(String companyName) {
            this.companyName = companyName;
            return this;
        }

        public ConcreteBuilder setOtherData(String otherData) {
            this.otherData = otherData;
            return this;
        }

        public InsuranceContract build() {
            if (contractId == null || contractId.length() == 0) {
                throw new IllegalArgumentException("合同编号不能为空");
            }
            boolean signPerson = personName != null && personName.trim().length() > 0;
            boolean signCompany = companyName != null && companyName.trim().length() > 0;
            if (signCompany && signPerson) {
                throw new IllegalArgumentException("一份合同不能同时与人和公司签订");
            }
            if (!signCompany && !signPerson) {
                throw new IllegalArgumentException("一份合同不能没有签订对象");
            }
            if (beginDate <= 0) {
                throw new IllegalArgumentException("合同必须要生效时间");
            }
            if (endDate <= 0) {
                throw new IllegalArgumentException("合同必须要失效时间");
            }
            if (endDate <= beginDate) {
                throw new IllegalArgumentException("合同失效日期必须大于开始日期");
            }

            return new InsuranceContract(this);
        }
    }

    public void someOperation() {
        System.out.println("someOperation->contractId is = " + this.contractId);
    }
}
